/**
 * @Author YuanYuan
 * @Date 2022/11/22
 * @Time 15:16
 */

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 * @return
 *
作业内容
分别实现这两种 body 格式的处理:
aa=10&bb=20
{ "aa": 10, "bb":20}
把 aa 和 bb 的值获取到, 展示到页面上.
提交核心代码, 和运行结果截图
*/
class Student {
    private int studentId;
    private String studentName;

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
}
@WebServlet("/json")
public class JsonPostServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = objectMapper.readValue(req.getInputStream(),Student.class);
        System.out.println( student.getStudentId() + " : " + student.getStudentName());
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write( student.getStudentId() + ", " + student.getStudentName());

    }
}
